/*
 * @Descripttion: 封装axios
 * @version: 0.0.0.1
 * @Author: chenyf15482
 * @Date: 2022-12-21 17:17:13
 * @LastEditors: chenyf15482
 * @LastEditTime: 2022-12-27 09:59:44
 */
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';

export type { InternalAxiosRequestConfig as requestProp, AxiosResponse as responseProp }

/**
 * @msg 通过构造器方法 构建请求连接
 *
 * @param {AxiosRequestConfig} config
 * @return {AxiosInstance} axios
 */
export function _linkCreate(config: AxiosRequestConfig): AxiosInstance {
    return axios.create(config);
}

/**
 * @msg 重复请求取消处理
 *
 * @param {string} URL
 */
type urlRecord = {url: string, cancel: () => void };
const pending: urlRecord[] = [];
export function _cancelLink(url: string) {
    for (const p in pending) {
        if (pending[p].url === url) {
            pending[p].cancel();
            pending.splice(Number(p), 1);
        }
    }
    new axios.CancelToken(c => {
        pending.push({ url, cancel: c });
    });
}

export default {
    _linkCreate,
    _cancelLink
}

